﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Data;

namespace ASV.Classes
{
    public class Connection
    {
        private string server;
        private string username;
        private string password;
        private string database;
        private MySql.Data.MySqlClient.MySqlConnection connection;

        public Connection()
        {
            server = "db4free.net";
            username = "nettma";
            password = "software";
            database = "softwarev";
            /*server = "127.0.0.1";
            username = "root";
            password = "";
            database = "software_verwaltung";*/
            this.connectDB(); 
        }

        public Connection(string server, string username, string password, string database)
        {
            this.setServer(server);
            this.setUsername(username);
            this.setPassword(password);
            this.setDatabase(database);
        }

        public void setServer(string server)
        {
            this.server = server;
        }

        public string getServer()
        {
            return this.server;
        }

        public void setUsername(string username)
        {
            this.username = username;
        }

        public string getUsername()
        {
            return this.username;
        }

        public void setPassword(string password)
        {
            this.password = password;
        }

        public string getPassword()
        {
            return this.password;
        }

        public void setDatabase(string database)
        {
            this.database = database;
        }

        public string getDatabase()
        {
            return this.database;
        }

        /**
         * Verbindung zur Datenbank aufbauen
         */
        public Boolean connectDB()
        {
            string myConnectionString;

            myConnectionString = "server=" + this.server + ";uid=" + this.username + ";pwd=" + this.password + ";database=" + this.database + ";";

            try
            {
                this.connection = new MySql.Data.MySqlClient.MySqlConnection();
                connection.ConnectionString = myConnectionString;
                connection.Open();
                if (connection.State.ToString() == "Open")
                    return true;
                else
                    return false;
            }
            catch (MySql.Data.MySqlClient.MySqlException ex)
            {
                return false;
            }
        }

        public Boolean connectionOpen()
        {
            if (connection.State.ToString() == "Open")
                return true;
            else
                return false;
        }

        public void closeConnection()
        {
            if (connectionOpen())
            {
                this.connection.Close();
            }
        }

        public void insertUpdate(String insertStr)
        {
            MySqlCommand insertCommand = new MySqlCommand(insertStr, this.connection);
            int i = insertCommand.ExecuteNonQuery();
        }

        public DataTable select(String selectStr)
        {
            MySqlCommand readCommand = new MySqlCommand(selectStr, this.connection);
            MySqlDataAdapter adapter = new MySqlDataAdapter(readCommand);
            DataTable datatable = new DataTable();

            adapter.Fill(datatable);
            return datatable;
        }
        public MySqlConnection getConnection()
        {
            return this.connection;
        }

    }
}